/**
   DanmuController
 * Time:2018年6月13日
 */
package com.ssm.controller;

import java.io.IOException;
import java.io.OutputStreamWriter;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ssm.entity.Danmu;
import com.ssm.entity.DanmuExample;
import com.ssm.entity.Usermessage;
import com.ssm.entity.vo.DanmuSearchVo;
import com.ssm.entity.vo.DanmuVo;
import com.ssm.service.DanmuService;

/**
 * @author 15199
 *
 */

@Controller
@RequestMapping("/Danmu")
public class DanmuController {

	@Resource
	private DanmuService danmuService;

	@ResponseBody
	@RequestMapping("/addDanmu.do")
	/*时间类型的json数据 不能直接转为对象*/
	public String addDanmu(@RequestBody DanmuVo danmuvo,HttpServletRequest request,HttpServletResponse response) throws ParseException{

		Usermessage usermessage=(Usermessage) request.getSession().getAttribute("usermessage");


		Integer id=(Integer) request.getSession().getAttribute("videoid");


		int userid = usermessage.getUserid();




		Danmu danmu=new Danmu();

		danmu.setUserid(userid);

		danmu.setVideoid(id);

		SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		//parse 转为date format转为string
		Date date=sdf.parse(danmuvo.getDate());

		danmu.setDate(date);

		danmu.setText(danmuvo.getText());;


		int n=danmuService.saveDanmu(danmu);          

		if(n>0){
			return "true";
		}else{
			return "false";
		}


	}

	@ResponseBody
	@RequestMapping("/showDanmu.do")
	public List<String> showDanmu(@RequestBody DanmuSearchVo danmuSearchVo,HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException{
		/*将弹幕发送给前端*/	
		String danmu=null;
        /*@ResponseBody也可以直接返回list对象*/
		List<Danmu> list=danmuService.getDanmu(danmuSearchVo);
		List<String> list1=new ArrayList<String>();
		for (Danmu danmu2 : list) {

			list1.add(danmu2.getText());


		}
		return list1;

	}







}
